import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TreeMap;

import javax.swing.JPanel;
import javax.swing.Timer;

import poset.SphericalPosetSpace;
import poset.User;
import spaces.VectorNode;
import vectors.Vector;


@SuppressWarnings("serial")
public class SphericalPosetSpaceViewer extends PosetSpaceViewer {
	
	private static final int NODE_RAD = 5;

	public SphericalPosetSpaceViewer(SphericalPosetSpace ps){
		super(ps);
	}
	
	public void paint(Graphics g){
		super.paint(g);
		
		int minSize = Math.min(getWidth(),getHeight());
		g.setColor(Color.DARK_GRAY);
		g.drawOval( getWidth()/2 - minSize/2, getHeight()/2 - minSize/2, minSize, minSize);
		
		TreeMap<Double,VectorNode> userPoses = new TreeMap<Double,VectorNode>();
		TreeMap<Double,VectorNode> itemPoses = new TreeMap<Double,VectorNode>();
		for(VectorNode node : ((SphericalPosetSpace)posetSpace).getSphericalSurfaceSpace().getNodes()){
			Vector curPos = node.getPos();
			if(node instanceof User){
				userPoses.put(curPos.getValue(2)+Math.random()*0.00000001, node);
			} else {
				itemPoses.put(curPos.getValue(2)+Math.random()*0.00000001, node);
			}
		}
		for(Double pos : userPoses.keySet()){
			int fade = (int)(pos*100+155);
			g.setColor(new Color(fade,0,0));
			g.fillOval( (int)(userPoses.get(pos).getPos().getValue(0)*minSize/2)+getWidth()/2 - NODE_RAD,
						(int)(userPoses.get(pos).getPos().getValue(1)*minSize/2)+getHeight()/2 - NODE_RAD,
						NODE_RAD*2, NODE_RAD*2);
			g.drawString( userPoses.get(pos).getName(),
						  (int)(userPoses.get(pos).getPos().getValue(0)*minSize/2)+getWidth()/2 + NODE_RAD,
						  (int)(userPoses.get(pos).getPos().getValue(1)*minSize/2)+getHeight()/2 + NODE_RAD);
		}
		for(Double pos : itemPoses.keySet()){
			int fade = (int)(pos*100+155);
			g.setColor(new Color(0,fade,0));
			g.fillOval( (int)(itemPoses.get(pos).getPos().getValue(0)*minSize/2)+getWidth()/2 - NODE_RAD,
						(int)(itemPoses.get(pos).getPos().getValue(1)*minSize/2)+getHeight()/2 - NODE_RAD,
						NODE_RAD*2, NODE_RAD*2);
			g.drawString( itemPoses.get(pos).getName(),
					  (int)(itemPoses.get(pos).getPos().getValue(0)*minSize/2)+getWidth()/2 + NODE_RAD,
					  (int)(itemPoses.get(pos).getPos().getValue(1)*minSize/2)+getHeight()/2 + NODE_RAD);
		}
		g.setColor(Color.YELLOW);
		g.drawString( "Tension: "+posetSpace.getTension(), 5, getHeight()-5 );
	}
}
